package com.zyh.controller;

import com.github.pagehelper.PageInfo;
import com.zyh.entity.Category;
import com.zyh.entity.Tea;
import com.zyh.service.CategoryService;
import com.zyh.service.TeaService;
import com.zyh.service.impl.CategoryServiceImpl;
import com.zyh.service.impl.TeaServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tea")
public class TeaController {
    ModelAndView mav = null;
    TeaService teaService = null;
    @RequestMapping("/selectAllTeaMvc")
    public ModelAndView selectAllTeaMvc(HttpSession httpSession,String curPageStr, @RequestParam(value = "nameSearch",defaultValue = "",required = false) String nameSearch, @RequestParam(value = "priceSearch",defaultValue = "",required = false) String priceSearch, @RequestParam(value = "categoryIdSearch",defaultValue = "",required = false) Integer categoryIdSearch){
        mav = new ModelAndView();
        teaService = new TeaServiceImpl();
        PageInfo<Tea> teaPageInfo = null;

        int curPage = 1;
        if(curPageStr!=null){
            curPage = Integer.parseInt(curPageStr);
        }
        int pageSize = 5;
        if(categoryIdSearch!=null){
            httpSession.setAttribute("categoryIdSearchSession",categoryIdSearch);
        }
        if(nameSearch!=null){
            httpSession.setAttribute("nameSearchSession",nameSearch);
        }
        if(priceSearch!=null){
            httpSession.setAttribute("priceSearchSession",priceSearch);
        }
        if(httpSession.getAttribute("categoryIdSearchSession")!=null || httpSession.getAttribute("nameSearchSession")!="" || httpSession.getAttribute("priceSearchSession")!=""){
            teaPageInfo = teaService.selectAllTea(curPage, pageSize ,(String) httpSession.getAttribute("nameSearchSession") ,(String) httpSession.getAttribute("priceSearchSession"),(Integer) httpSession.getAttribute("categoryIdSearchSession"));
        }else{
            teaPageInfo = teaService.selectAllTea(curPage, pageSize , nameSearch ,priceSearch,categoryIdSearch);
        }
        mav.addObject("teaPageInfo",teaPageInfo);
        mav.setViewName("/tealist.jsp");
        return mav;
    }
    @RequestMapping("/insertOneMvc")
    public ModelAndView insertOneMvc(Tea tea, String categoryName,HttpSession httpSession){
        mav = new ModelAndView();
        teaService = new TeaServiceImpl();
        CategoryService categoryService = new CategoryServiceImpl();
        Category category = categoryService.selectOneByName(categoryName);

        tea.setCategoryId(category.getCategoryIdMy());
        tea.setAddTime(new Date());
        tea.setCategory(category);
        teaService.insertOne(tea);
        mav.setViewName("redirect:/tea/refreshMvc.do");
        return mav;
    }

    @RequestMapping("/deleteTeasMvc")
    public ModelAndView deleteTeasMvc(int[] ids){
        mav = new ModelAndView();
        teaService = new TeaServiceImpl();
        teaService.deleteTeas(ids);
        mav.setViewName("redirect:/tea/selectAllTeaMvc.do");
        return mav;
    }

    @RequestMapping("/selectOneByIdMvc")
    public ModelAndView selectOneByIdMvc(Integer teaId){
        mav = new ModelAndView();
        teaService = new TeaServiceImpl();
        Tea tea = teaService.selectOneById(teaId);
        CategoryService categoryService = new CategoryServiceImpl();
        List<Category> categoryList = categoryService.selectAll();
        mav.addObject("categoryList",categoryList);
        mav.addObject("tea",tea);
        mav.setViewName("/updateTea.jsp");
        return mav;
    }

    @RequestMapping("/updateTeaMvc")
    public ModelAndView updateTeaMvc(Tea tea,String categoryName){
        mav = new ModelAndView();
        teaService = new TeaServiceImpl();
        CategoryService categoryService = new CategoryServiceImpl();
        Category category = categoryService.selectOneByName(categoryName);
        tea.setCategoryId(category.getCategoryIdMy());
        tea.setAddTime(new Date());
        teaService.updateTea(tea);
        mav.setViewName("/tea/selectAllTeaMvc.do");
        return mav;
    }

    @RequestMapping("/deleteTeaByIdMvc")
    public ModelAndView deleteTeaByIdMvc(Integer teaId){
        mav = new ModelAndView();
        teaService = new TeaServiceImpl();
        teaService.deleteTeaById(teaId);
        mav.setViewName("/tea/selectAllTeaMvc.do");
        return mav;
    }

    @RequestMapping("/refreshMvc")
    public ModelAndView refreshMvc(HttpSession httpSession){
        httpSession.removeAttribute("categoryIdSearchSession");
        httpSession.removeAttribute("nameSearchSession");
        httpSession.removeAttribute("priceSearchSession");
        mav.setViewName("/tea/selectAllTeaMvc.do");
        return mav;
    }
}
